# coding=utf-8

from web.utils import storage

import utils
from db import connection

cdb = connection.crawl

class Spider(object):
    """
    蜘蛛相关的数据层方法
    """
    @staticmethod
    def get(sid):
        """获取给定的蜘蛛记录"""
        sets = cdb.select('spider', where = 'id = $sid', vars = locals())
        return sets and sets[0] or None
    
    @staticmethod
    def get_all_for_dropdown():
        """
        获取所有的蜘蛛记录，并输出一个dropdown的格式
        """
        sets = cdb.select('spider', order = 'id DESC')
        ret = sets and [(s.id, s.name) for s in sets] or None
        if ret:
            ret.insert(0, ('', ''))
        return ret

    @staticmethod
    def get_all(offset, length):
        """分页获取所有蜘蛛"""
        sets = cdb.query('''
                SELECT SQL_CALC_FOUND_ROWS *
                FROM spider 
                ORDER BY id DESC
                LIMIT $offset, $length;
                '''
                , vars = locals())
        totals = cdb.query('SELECT FOUND_ROWS() AS totals;')
        return storage(lst = sets, totalrecords = totals[0].totals)

    @staticmethod
    def add(form):
        """添加蜘蛛"""
        return cdb.insert('spider'
                , name = form.name.value
                , partner_id = form.partner_id.value)

    @staticmethod
    def update(sid, form):
        """更新给定蜘蛛"""
        return cdb.update('spider'
                , name = form.name.value
                , partner_id = form.partner_id.value
                , where = 'id = $sid'
                , vars = locals())

class Hotel(object):
    """酒店相关数据接口"""
    @staticmethod
    def get_all(offset, length):
        """分页获取所有酒店规则"""
        sets = cdb.query('''
                SELECT SQL_CALC_FOUND_ROWS h.*,s.name 
                FROM spider_hotel AS h
                    INNER JOIN
                    spider AS s
                        ON s.id = h.spider_id
                ORDER BY h.id DESC
                LIMIT $offset, $length;
                '''
                , vars = locals())
        totals = cdb.query('SELECT FOUND_ROWS() AS totals;')
        return storage(lst = sets, totalrecords = totals[0].totals)

    @staticmethod
    def get(hid):
        """获取给定酒店的规则"""
        sets = cdb.select('spider_hotel'
                , where = 'id = $hid'
                , vars = locals())
        return sets and sets[0] or None

    @staticmethod
    def add(form):
        """添加酒店路径"""
        return cdb.insert('spider_hotel'
                , spider_id = form.spider_id.value
                , dest_id = form.dest_id.value
                , star = utils.intval(form.stars.value)
                , url_regex = form.url_regex.value)

    @staticmethod
    def update(hid, form):
        """更新酒店路径"""
        return cdb.update('spider_hotel'
                , spider_id = form.spider_id.value
                , dest_id = form.dest_id.value
                , star = utils.intval(form.stars.value)
                , url_regex = form.url_regex.value
                , where = 'id = $hid'
                , vars = locals())
